// pages/adressMana/adressMana.js
var app = getApp();
var radio = require('../../utils/main.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    isMana:false,
    adressList:'',
    productId: '',
    propertyId:'',
    amount: 1,
    loadSuccess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // radio.changeRadio.apply(this, []);
    var address = wx.getStorageSync('address');
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      productId: address.productId,
      amount: address.amount,
      propertyId: address.propertyId
    })
    if (options.isMana!=undefined) {
      wx.setNavigationBarTitle({
        title: '管理地址'
      })
      var isMana = options.isMana == 'true' ? true : false;
      this.setData({ isMana: isMana});
    }
    
  },
  // 查询收货地址
  getAddress: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'userAddress/page',
      data: { userId: this.data.userInfo.id },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        _this.setData({
          loadSuccess: true
        })
        wx.hideLoading()
        if (res.data.success) {
          _this.setData({
            adressList: res.data.data.pp
          })
        }
      }
    })
  },
  // 管理地址
  adressMana:function(e){
    wx.navigateTo({
      url: '/pages/adressMana/adressMana?isMana=true' + '&productId=' + this.data.productId + ' &amount=' + this.data.amount + '&propertyId' + this.data.propertyId,
    })
  },
  // 添加地址
  addAdress: function (e) {
    wx.navigateTo({
      url: '/pages/addAdress/addAdress',
    })
  },
  // 编辑地址
  editAdress:function(e){
    wx.navigateTo({
      url: '/pages/addAdress/addAdress?editData=' + JSON.stringify(this.data.adressList[e.currentTarget.dataset.value]),
    })
  },
  // 删除地址
  delAdress:function(e){
    var _this = this;
    if (_this.data.adressList.length > 1){
      wx.request({
        url: app.globalData.httpApiCar + 'userAddress/del',
        data: {
          id: e.currentTarget.dataset.id,
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            wx.redirectTo({
              url: '/pages/adressMana/adressMana?isMana=false'
            })
          }
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
    
    
  },
  selAdress: function (e) {
    wx.redirectTo({
      url: '/pages/confirmOrder/confirmOrder?adressData=' + JSON.stringify(this.data.adressList[e.currentTarget.dataset.value]) + '&productId=' + this.data.productId + ' &amount=' + this.data.amount,
    })
    //this.data.adressList.splice(e.currentTarget.dataset.value, 1);
    // this.setData({
    //   adressList: this.data.adressList
    // })
  },
  // 选择默认地址
  changeDefault: function (e) {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'userAddress/default',
      data: {
        id: e.currentTarget.dataset.id,
        userId: _this.data.userInfo.id
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        wx.redirectTo({
          url: '/pages/adressMana/adressMana?isMana=false'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading()
    this.getAddress()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})